package com.filldream.fastboot.common.util;



/**
 * 地图 工具类
 * @author RickSun
 *
 */
public class MapUtil {

    public static void main(String[] args) {
        double distance = distance(113.219633D, 28.189158D, 113.032605D, 28.185351D);
        System.out.println(distance);
    }

	/**
     * 计算地球上任意两点(经纬度)距离
     *
     * @param long1 第一点经度
     * @param lat1  第一点纬度
     * @param long2 第二点经度
     * @param lat2  第二点纬度
     * @return 返回距离 单位：米
     */
    public final static double distance(double long1, double lat1, double long2, double lat2) {
        double a, b, R;
        R = 6378137; // 地球半径
        lat1 = lat1 * Math.PI / 180.0;
        lat2 = lat2 * Math.PI / 180.0;
        a = lat1 - lat2;
        b = (long1 - long2) * Math.PI / 180.0;
        double d;
        double sa2, sb2;
        sa2 = Math.sin(a / 2.0);
        sb2 = Math.sin(b / 2.0);
        d = 2
                * R
                * Math.asin(Math.sqrt(sa2 * sa2 + Math.cos(lat1)
                * Math.cos(lat2) * sb2 * sb2));
        return d;
    }


//    /**
//     * 百度地图-根据IP获取地址
//     * @param ip    ip地址
//     * @param key   百度key
//     * @return
//     * @throws IOException
//     */
//    public static LocationByIp getBaiduAddrByIp(String ip,String key)throws IOException{
//        String url ="https://api.map.baidu.com/location/ip?ak=" + key + "&coor=bd09ll&ip=" + ip;
//        String json = UrlUtil.doGet(url);
//        LocationByIp result = new LocationByIp();
//
//
//        JSONObject jsonObject = JsonUtil.fromJson(json);
//        JSONObject content = jsonObject.getJSONObject("content");
//        JSONObject address_detail = content.getJSONObject("address_detail");
//
//        result.setStatus(content.getInteger("status"));
//        result.setAddress(content.getString("address"));
//
//        result.setProvince(address_detail.getString("province"));
//        result.setCity(address_detail.getString("city"));
//        result.setStreet(address_detail.getString("street"));
//        result.setStreetNumber(address_detail.getString("street_number"));
//        result.setDistrict(address_detail.getString("district"));
//        result.setCenterCityX(content.getJSONObject("point").getString("x"));
//        result.setCenterCityY(content.getJSONObject("point").getString("y"));
//
//
//        return result;
//    }

//    /**
//     * 百度地图-根据经纬度获取地址
//     * @param longitude 经度
//     * @param latitude  维度
//     * @param isCoorTransformation  是否坐标转换
//     * @param key   百度Key
//     * @return
//     * @throws IOException
//     */
//    public static V2 getBaiduAddrByCoor(String longitude, String latitude, Boolean isCoorTransformation, String key) throws IOException {
//        V2 v2 = new V2();
//        Map<String,String> map = new HashMap<>();
//        map.put("coords",longitude+","+latitude);
//        map.put("from",1+"");
//        map.put("to",5+"");
//        map.put("ak", key);
//        String json = "{\"status\":-1}";
//
//        if(isCoorTransformation){
//            //坐标转换
//            String baiduResult =  UrlUtil.doGet("http://api.map.baidu.com/geoconv/v1/", map);
//            JSONObject addJson = JsonUtil.fromJson(baiduResult);
//
//            JSONArray addresschansResult = addJson.getJSONArray("result");
//            JSONObject locat = (JSONObject) addresschansResult.getJSONObject(0);
//            longitude = locat.getString("y");
//            latitude = locat.getString("x");
//            json = UrlUtil.doGet("http://api.map.baidu.com/geocoder/v2/?ak=" + key
//                    + "&location=" + longitude + "," + latitude + "&output=json&pois=0");
//        }else{
//            json = UrlUtil.doGet("http://api.map.baidu.com/geocoder/v2/?ak=" + key
//                    + "&location=" + latitude + "," + longitude + "&output=json&pois=0");
//        }
//
//
//        JSONObject address = JsonUtil.fromJson(json);
//        JSONObject addressResult = address.getJSONObject("result");
//        JSONObject addressComponent = addressResult.getJSONObject("addressComponent");
//
//        v2.setStatus(address.getInteger("status"));
//        v2.setFormattedAddress(addressResult.getString("formatted_address"));
//        v2.setBusiness(addressResult.getString("business"));
//        v2.setSematicDescription(addressResult.getString("sematic_description"));
//
//        v2.setCountry(addressComponent.getString("country"));
//        v2.setCountryCode( addressComponent.getString("country_code_iso") + "|" + addressComponent.getString("country_code_iso2"));
//        v2.setProvince(addressComponent.getString("province"));
//        v2.setCity(addressComponent.getString("city"));
//        v2.setCityLevel(addressComponent.getInteger("city_level"));
//        v2.setDistrict(addressComponent.getString("district"));
//        v2.setAdcode(addressComponent.getString("adcode"));
//        v2.setStreet(addressComponent.getString("street"));
//        v2.setStreetNumber(addressComponent.getString("street_number"));
//        return v2;
//    }
    
    

}
